package
{
	import com.greensock.TweenLite;
	import com.netease.protobuf.UInt64;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.utils.Dictionary;
	
	import modules.majiang.viewManager.MajiangUIManager;

	/**
	 *
	 * @author xumin.xu 2013-1-15
	 */

	public class UIAllRefer
	{
		public static var stage:Stage;

		/**
		 * 提示框层
		 */
		public static const tipLayer:Sprite = new Sprite();

		/**
		 * 提示信息层
		 */
		public static const infoLayer:Sprite = new Sprite();

		/**
		 * 弹窗层
		 */
		public static const popLayer:Sprite = new Sprite();

		/**
		 * 内容层
		 */
		public static const contentLayer:Sprite = new Sprite();

		/**
		 * 背景层
		 **/
		public static const backLayer:Sprite = new Sprite();

		private static var _instance:UIAllRefer;

		public static function get instance():UIAllRefer
		{
			if (_instance == null)
			{
				_instance = new UIAllRefer();
			}
			return _instance;
		}

		/**
		 * 游戏主界面
		 */
		public var mainView:Game;

		/**
		 * 播放切换桌子特效
		 **/
		public static function showStageMask(tableId:UInt64):void
		{
			if (UIAllRefer.instance.getGameUIbyTableId(tableId).visible)
			{
				var gameUIMask:Sprite = getGameUIMask(tableId);

				if (UIAllRefer.instance.getGameUIbyTableId(tableId).parent)
				{
					UIAllRefer.instance.getGameUIbyTableId(tableId).parent.addChild(gameUIMask);
				}

				var startObj:Object = {};
				startObj.width = 1000;
				startObj.height = 650;

				//使用特效类
				var tweenObj:Object = {};
				tweenObj.width = 5;
				tweenObj.height = 5;
				tweenObj.x = stage.width / 2;
				tweenObj.y = stage.height / 2;

				tweenObj.onUpdate = function(disO:Sprite, updateObj:Object):void
				{
					disO.graphics.clear();
					disO.graphics.beginFill(0x0);
					disO.graphics.drawRect((1000 - updateObj.width) / 2, (650 - updateObj.height) / 2, updateObj.width, updateObj.height);
					disO.graphics.endFill();
				}
				tweenObj.onUpdateParams = [gameUIMask, startObj];

				tweenObj.onComplete = function(disO:Sprite):void
				{
					var startObj1:Object = {};
					startObj1.width = disO.width;
					startObj1.height = disO.height;

					var tweenObj1:Object = {};
					tweenObj1.width = 1000;
					tweenObj1.height = 650;

					tweenObj1.onUpdate = function(disO:Sprite, updateObj:Object):void
					{
						disO.graphics.clear();
						disO.graphics.beginFill(0x0);
						disO.graphics.drawRect((1000 - updateObj.width) / 2, (650 - updateObj.height) / 2, updateObj.width, updateObj.height);
						disO.graphics.endFill();
					}
					tweenObj1.onUpdateParams = [disO, startObj1];

					tweenObj1.onComplete = function(disO:DisplayObject):void
					{
						if (disO.parent)
						{
							disO.parent.removeChild(disO);
						}
					}
					tweenObj1.onCompleteParams = [disO];

					TweenLite.to(startObj1, 2.0, tweenObj1);
				};
				tweenObj.onCompleteParams = [gameUIMask];

				TweenLite.to(startObj, 1.0, tweenObj);

			}
		}

		private static var _gameUIMask:Sprite;

		/**
		 * 创建一个色块覆盖在stage上，换桌时显示动画
		 **/
		private static function getGameUIMask(tableId:UInt64):Sprite
		{
			if (_gameUIMask == null)
			{
				_gameUIMask = new Sprite();
				UIAllRefer.instance.getGameUIbyTableId(tableId).mask = _gameUIMask;
			}
			return _gameUIMask;
		}

		/**
		 * 桌子字典， tableID作为key ,
		 * gameUI 放到mainView上
		 */
		public var mGameUiDic:Dictionary = new Dictionary();

		public function getGameUIbyTableId(tableId:UInt64):MajiangUIManager
		{
			var majiangUIManager:MajiangUIManager = mGameUiDic[tableId.toString()];
			if (majiangUIManager == null)
			{
				majiangUIManager = new MajiangUIManager(tableId);
				majiangUIManager.close();
				mGameUiDic[tableId.toString()] = majiangUIManager;
			}
			return majiangUIManager;
		}

		public function getCurrentShowGameUi():MajiangUIManager
		{
			for each (var value:MajiangUIManager in mGameUiDic)
			{
				if (value.visible == true)
					return value;
			}
			return null;
		}

		public function updateMoreGameUI():void
		{
			var index:int;
			for each (var gameUI:MajiangUIManager in UIAllRefer.instance.mGameUiDic)
			{
				if (gameUI.visible == false)
					continue;
				gameUI.close();
				gameUI.mainView.x = index % 2 * (gameUI.mainView.width); //update的时候,width属性已经改变了
				gameUI.mainView.y = int(index / 2) * gameUI.mainView.height + 30;
				index++;
				if (index == 4)
					break;
			}
		}

		public function resetGameUi():void
		{
			for each (var gameUI:MajiangUIManager in mGameUiDic)
			{
				gameUI.mainView.scaleX = 1;
				gameUI.mainView.scaleY = 1;
				gameUI.mainView.x = 0;
				gameUI.mainView.y = 0;
			}
		}

		public function showMoreGameUI():void
		{
			var index:int;
			for each (var gameUI:MajiangUIManager in mGameUiDic)
			{
				gameUI.mainView.scaleX = 0.5;
				gameUI.mainView.scaleY = ((gameUI.mainView.height - 30) >> 1) / gameUI.mainView.height;
				gameUI.mainView.visible = true;
				gameUI.mainView.x = index % 2 * (gameUI.mainView.width >> 1); //scale虽然变了,但是width属性并未改变
				gameUI.mainView.y = int(index / 2) * ((gameUI.mainView.height - 30) >> 1) + 30;
				index++;
				gameUI.roomIndex = index;
				if (index == 4)
					break;
			}
		}

	}
}
